package com.yc.chatroom.ui.client;

import java.awt.event.TextEvent;
import java.io.IOException;
import java.util.Scanner;

import com.yc.chatroom.client.ClientManager;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.FlowPane;
import javafx.stage.Stage;

public class MainApplication extends Application {
	
	ClientManager mainClient = null;
	
	public Stage primaryStage;
	TextArea sendMsg_TextArea = null;
	TextField sendMsg_TextFiled = null;
	
	TextField login_TextFiled_UserName = null;
	TextField login_TextFiled_Password = null;
	
	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage primaryStage) throws Exception {
		this.primaryStage = primaryStage;
		
		//展示登录页面
		showSendView();
		
		//连接服务器
		mainClient = new ClientManager();
		try {
			mainClient.connect("localhost", 18181);
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	/**
	 * 登录页面
	 */
	public void showLoginWiew() {
		//设置窗口大小
		primaryStage.setTitle("login");
		primaryStage.setWidth(400);
		primaryStage.setHeight(300);
		
		//设置流式布局
		FlowPane fp = new FlowPane();
		
		//定义两个输入框
		login_TextFiled_UserName = new TextField();
		login_TextFiled_Password = new TextField();
		
		//定义一个登录按钮
		Button bt = new Button("登录");
		bt.setOnAction(new ButEventHandle_Login());//添加事件
		
		Scene sc = new Scene(fp);
		primaryStage.setScene(sc);
		primaryStage.show();
	}
	
	/**
	 * 发送消息页面
	 */
	public void showSendView() {
		//设置窗口大小
		primaryStage.setTitle("demo");
		primaryStage.setWidth(400);
		primaryStage.setHeight(300);
		
		//定义一个面板布局
		BorderPane bp = new BorderPane();
		
		//定义一个多行输入框
		sendMsg_TextArea = new TextArea();
		
		//定义一个输入框
		sendMsg_TextFiled = new TextField();
		
		//定义一个按钮
		Button bt = new Button("发送");
		bt.setOnAction(new ButEventHandle_SendMsg());//添加事件
		
		bp.setCenter(sendMsg_TextArea);//多行输入框放到中间
		bp.setBottom(sendMsg_TextFiled);//输入框放到底部
		bp.setRight(bt);//按钮放到右边
		
		Scene sc = new Scene(bp);
		primaryStage.setScene(sc);
		primaryStage.show();
	}
	
	
	class ButEventHandle_SendMsg implements EventHandler<ActionEvent> {

		@Override
		public void handle(ActionEvent event) {
			//获取要发送的内容
			String sendMsg = sendMsg_TextFiled.getText();
			//调用client发送接口
			if (null != mainClient) {
//				mainClient.sendMsg(msg);
			}
			
		}
	}
	
	class ButEventHandle_Login implements EventHandler<ActionEvent> {

		@Override
		public void handle(ActionEvent event) {
			//获取要发送的内容
			String sendMsg = sendMsg_TextFiled.getText();
			//调用client发送接口
			
			
		}
	}
	

}
